September 17, 2021
Parametric Search
import java.io.*;
import java.util.*;
public class Main {
static FastReader scan = new FastReader();
static StringBuilder sb = new StringBuilder();
static int N, M;
static int[] A;
static void input() {
N = scan.nextInt();
M = scan.nextInt();
A = new int[N + 1];
for (int i = 1; i <= N; i++) {
A[i] = scan.nextInt();
}
}
static boolean gotEnoughTrees(int H) {
// long 주의!
long sum = 0;
for (int i = 1; i <= N; i++) {
// 자를 나무가 있을 때
if (A[i] > H) {
sum += A[i] - H;
}
}
return sum >= M;
}
static void binarySearch() {
int L = 0;
int R = 2_000_000_000;
long answer = 0;
while (L <= R) {
int mid = (L + R) / 2;
if (gotEnoughTrees(mid)) {
answer = mid;
L = mid + 1;
} else {
R = mid - 1;
}
}
System.out.println(answer);
}
public static void main(String[] args) {
input();
binarySearch();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}